package com.example.demo.service;

import com.example.demo.dao.TBizAuctionArtDAO;
import com.example.demo.dao.TBizAuctionDAO;
import com.example.demo.po.*;
import com.example.demo.util.PageUtils;
import com.example.demo.util.SeqFactory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;

@Service
public class TBizAuctionService {
    @Autowired
    TBizAuctionDAO tBizAuctionDAO;
    @Autowired
    TBizAuctionArtDAO tBizAuctionArtDAO;
    /**
     * 分页查询接口
     * 这里统一封装了分页请求和结果，避免直接引入具体框架的分页对象, 如MyBatis或JPA的分页对象
     * 从而避免因为替换ORM框架而导致服务层、控制层的分页接口也需要变动的情况，替换ORM框架也不会
     * 影响服务层以上的分页接口，起到了解耦的作用
     * @param pageRequest 自定义，统一分页查询请求
     * @return PageResult 自定义，统一分页查询结果
     */
    public PageResult findByPage(TBizAuction auction, PageRequest pageRequest){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<TBizAuction> list = tBizAuctionDAO.findByParams(auction);
        PageResult pageResult = PageUtils.getPageResult(pageRequest, new PageInfo<TBizAuction>(list));
        return pageResult;
    }

    /**
     * app端查询未结束的拍卖会
     * @param auction
     * @param pageRequest
     * @return
     */
    public PageResult appaucList(TBizAuction auction, PageRequest pageRequest){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<TBizAuction> list = tBizAuctionDAO.appaucList(auction);
        PageResult pageResult = PageUtils.getPageResult(pageRequest, new PageInfo<TBizAuction>(list));
        return pageResult;
    }



    public TBizAuction findById(Integer id){
        return tBizAuctionDAO.findById(id);
    }

    public TBizAuction findByaid(String auctionid){
        return tBizAuctionDAO.findByaid(auctionid);
    }

    @Transactional
    public boolean saveOrUpdate(TBizAuction auction, HttpServletRequest request){
        UserManage user =(UserManage)request.getSession().getAttribute("session_user");
        int i = 0;

        if(auction.getId()!=null){
            auction.setUpdatetime(new Date());
            auction.setUpdateuser(user.getId().toString());
            i = tBizAuctionDAO.update(auction);
        }else{
            String auctionCode ="AUC"+ SeqFactory.getNewSequenceAlone();
            auction.setAuctionid(auctionCode);
            auction.setCreatetime(new Date());
            auction.setCreateuser(user.getId().toString());
            i= tBizAuctionDAO.save(auction);
        }
        if(i>0)
            return true;
        else
            return false;
    }

    @Transactional
    public  boolean del(String auctionid){
        int i = tBizAuctionDAO.del(auctionid);
        if(i>0){
            TBizAuctionArt aa = new TBizAuctionArt();
            aa.setAuctionid(auctionid);
            int y =tBizAuctionArtDAO.del(aa);
            if(y>0)
                return true;
            else
                return false;
        }else
            return false;
    }


}
